/* unipush 处理 */
module.exports = {

	methods: {
		/* 后台绑定cid */
		initPush() {
			let userInfo = this.$tools.getUserInfo();
			let info = plus.push.getClientInfo();
			console.log(info.clientid);
			let clientid = info.clientid;
			if (clientid && clientid.length > 4) {
				let params = {
					userId: userInfo.id,
					cid: clientid,
				};
				let opt = this.$appApi.getApi("addUserCid", params,true,false);
				//请求
				let reqeust = this.$tools.request(
					opt,
					(res) => {
						console.log("clientid==============");
						console.log(res);
					},
					(error) => {},
					false
				);
			}
		},
		/* 推送监听 */
		initPushListener() {
			// 消息点击
			plus.push.addEventListener('click', (msg) => {
				console.log("click:payload-->" + JSON.stringify(msg));
				this.brandSetup('click', msg);
			});
			//消息接收
			plus.push.addEventListener('receive', (msg) => {
				console.log("receive:payload-->" + JSON.stringify(msg));
				this.brandSetup('receive', msg);
			});
		},
		/* 不同品牌桌面图标的角标处理 */
		brandSetup(action, msg = {}) {
			let sysInfo = this.$tools.getSysInfo();
			//
			console.log(sysInfo);
			if (sysInfo.platform.toLowerCase() == 'ios') {
				if (action == 'click') {
					//点击的时候清空角标
					this.$tools.setBadgeNum(0);
					//点击的时候清空所有通知
					plus.push.clear();
				} else if (action == 'receive') {
					if (msg.aps == null && msg.type == 'receive') { //防止重复通知
						plus.push.createMessage(msg.content, msg.payload, {
							title: msg.title
						});
					}
				} else if (action == 'launch') {
					this.$tools.setBadgeNum(0);
				}
			} else if (sysInfo.platform.toLowerCase() == 'android') {
				let brand = sysInfo.brand.toLowerCase();
				if (brand.indexOf('xiaomi') != -1) {
					//小米手机：角标自动加1 （MIUI11 以下） 另：高版本MIUI 设置角标会以通知的形态发送
					if (action == 'click') {
						//点击的时候清空所有通知
						if (plus.push.getAllMessage() && plus.push.getAllMessage().length > 0) {
							plus.push.clear();

						}
					}
				} else if (brand.indexOf('huawei') != -1) {
					if (action == 'receive') {
						console.log('huawei push');
						if (plus.push.getAllMessage()) {
							let count = (plus.push.getAllMessage().length || 0) + 1
							this.$tools.setBadgeNum(count);
						}
					} else if (action == 'click') {
						//点击的时候清空角标
						this.$tools.setBadgeNum(0);
						//点击的时候清空所有通知
						plus.push.clear();
					} else if (action == 'launch') {
						this.$tools.setBadgeNum(0);
					}
				} else if (brand.indexOf('vivo') != -1) {
					if (action == 'click') {
						//点击的时候清空所有通知
						if (plus.push.getAllMessage() && plus.push.getAllMessage().length > 0) {
							plus.push.clear();
					
						}
					}
				} else if (brand.indexOf('oppo') != -1) {
					if (action == 'click') {
						//点击的时候清空所有通知
						if (plus.push.getAllMessage() && plus.push.getAllMessage().length > 0) {
							plus.push.clear();

						}
					}
				} else if (brand.indexOf('samsung') != -1) {
					if (action == 'click') {
						//点击的时候清空所有通知
						if (plus.push.getAllMessage() && plus.push.getAllMessage().length > 0) {
							plus.push.clear();

						}
					}
				}
			}
		}
	}
}
